package com.hwua.view;

import com.hwua.domain.Car;
import com.hwua.service.CarService;
import com.hwua.serviceimpl.CarServiceImpl;
import com.hwua.util.InputTools;

import java.util.List;

/**
 * 管理员查看租车记录界面
 * @author admin
 */
public class AdminLookRecordView extends AbstractView{
    private String returnTime;
    @Override
    public AbstractView showView() {
        CarService cs = new CarServiceImpl();
        List<Car> carList = cs.findRecord();
        System.out.println("===================================================================<<租车记录界面>>" +
                "============================================================================");
        System.out.println("编号\t汽车编号\t汽车名称\t每日租金\t总租金\t\t品牌\t\t类型\t\t备注\t\t用户名\t\t借车时间\t\t\t\t\t还车时间");
        for (Car car : carList) {
            if(car.getReturnTime()==null){
                returnTime="尚未归还";
                System.out.println(car.getRecordId()+"\t\t"+car.getCarId()+"\t\t\t"+car.getCarName()+"\t\t"+car.getDailyRent()+"\t\t"+car.getPayment()+
                        "\t\t\t"+car.getBrand()+"\t\t"+car.getType()+"\t\t"+car.getCarRemark()+"\t"+car.getUserName()
                        + "\t\t"+car.getRentTime()+"\t\t"+returnTime);
                continue;
            }
            System.out.println(car.getRecordId()+"\t\t"+car.getCarId()+"\t\t\t"+car.getCarName()+"\t\t"+car.getDailyRent()+"\t\t"+car.getPayment()+
                    "\t\t"+car.getBrand()+"\t\t"+car.getType()+"\t\t"+car.getCarRemark()+"\t"+car.getUserName()
                    + "\t\t"+car.getRentTime()+"\t\t"+car.getReturnTime());
        }
        System.out.println("=========================================================================" +
                "===================================================================================");
        System.out.println();
        System.out.println("输入0：           退出");
        System.out.println("输入1：           查看全部汽车");
        System.out.println("输入2+用户编号：  查看指定用户租赁记录");
        System.out.println("输入3+汽车编号：  查看指定汽车租赁记录");
       while (true){
           String choose = InputTools.nextString();
           //使用正则表达式匹配录入的字符串是否符合“1+1”或者“1” 格式
           boolean matches = choose.matches("[0-9]{1}(\\+\\d+)?");
           if(matches){
               //判断键盘录入的字符串长度是否大于1，如果大于1，则截取
               if(choose.length()>1) {
                   //通过String类的split方法分割字符串
                   String[] strings = choose.split("\\+");
                   //  获得键盘录入字符串中 + 号前面的字符并转换成int类型
                   int num1 = Integer.parseInt(String.valueOf(strings[0]));
                   //  键盘录入字符串中 + 号后面的字符并转换成int类型
                   int num2 = Integer.parseInt(String.valueOf(strings[1]));
                   switch (num1){
                       case 2:
                           nextView = new FindRecordByUserIdView(num2);
                           break;
                       case 3:
                           nextView = new FindRecordByCarIdView(num2);
                           break;
                       default:
                           System.out.println("超出可选范围，请重新选择：");
                           continue;
                   }
               }else{
                   switch (choose){
                       case "0":
                           nextView=null;
                           break;
                       case "1":
                           nextView = new AdminView();
                           break;
                       default:
                           System.out.println("超出可选范围，请重新选择：");
                           continue;
                   }
               }
               break;
           }else{
               System.out.println("输入有误，请重新输入：");
           }
       }
       return nextView;
    }
}
